package com.digua.base.view

import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.TimeUtils
import com.digua.base.R
import com.digua.base.base.Activity
import com.digua.base.dialog.PwdDialog
import com.digua.base.ui.activity.ProjectDocActivity
import com.digua.base.util.StrUtil
import com.digua.base.util.Toasts
import java.util.Date


/**
 * 项目开发文档
 */
class ProjectDocView : View, View.OnTouchListener {
    private var time = 0L
    private var url: String? = null//文档地址
    private var pwd: String? = null//打开文档密码
    private var isPwd = false//是否需要密码
    private var clickTime = 10_000//点击时长

    constructor(context: Context?) : super(context) {
        initData(null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initData(attrs)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initData(attrs)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        initData(attrs)
    }

    private fun initData(attrs: AttributeSet?) {
        setOnTouchListener(this)
        attrs?.let {
            val ta = context.obtainStyledAttributes(it, R.styleable.ProjectDocView)
            url = ta.getString(R.styleable.ProjectDocView_url)
            pwd = ta.getString(R.styleable.ProjectDocView_pwd)
            isPwd = ta.getBoolean(R.styleable.ProjectDocView_isPwd, false)
            clickTime = ta.getInt(R.styleable.ProjectDocView_clickTime, 10_000)
        }
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN) {
            time = System.currentTimeMillis()
            return StrUtil.isNotBlank(url)
        }
        if (event?.action == MotionEvent.ACTION_UP) {
            if (System.currentTimeMillis() - time > clickTime) {
                if (isPwd) {
                    val dialog = PwdDialog()
                    dialog.confirmListener = {
                        val time = TimeUtils.date2String(Date(), "HHmm")
                        if (it == pwd || it == time) {
                            openDoc()
                            true
                        } else {
                            Toasts.showInGravity(Gravity.TOP, R.string.pwd_error)
                            false
                        }
                    }
                    val activity = ActivityUtils.getTopActivity() as Activity<*>
                    dialog.show(activity.supportFragmentManager)
                } else {
                    openDoc()
                }
            }
        }
        return false
    }

    private fun openDoc() {
        val intent = Intent(context, ProjectDocActivity::class.java)
        intent.putExtra("url", url)
        context.startActivity(intent)
    }
}